<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="videoFormRules" :model="videoForm" label-position="left" label-width="140px">
      <el-form-item label="视频名称" prop="videoName">
        <el-input v-model="videoForm.videoName" />
      </el-form-item>
      <el-form-item label="视频图片" prop="logo">
        <Upload blockName="video" v-model="videoForm.logo" />
      </el-form-item>
      <el-form-item label="关键字" prop="keyword">
        <el-input v-model="videoForm.keyword" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="关键字" />
      </el-form-item>
      <el-form-item label="视频地址" prop="videoUrl">
        <SingleVideoUpload blockName="video" v-model="videoForm.videoUrl" />
      </el-form-item>
      <el-form-item label="时长" prop="duration">
        <el-input v-model="videoForm.duration" />
      </el-form-item>
      <el-form-item label="大小" prop="capacity">
        <el-input v-model="videoForm.capacity" />
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input v-model="videoForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        确认
      </el-button>
      <el-button v-if="videoForm.status.value === 5" type="danger" @click="handleDelete(videoForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveVideo, removeVideoByID, getVideoDetail } from '@/api/knowledge/video'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'
import Upload from '@/components/Upload/SingleImage2' // secondary package based on el-pagination
import SingleVideoUpload from '@/components/Upload/SingleVideoUpload' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: {Upload,SingleVideoUpload },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      videoForm: {
        code: '',
        title: '',
        remark: '',
        status: {
          label: '',
          value: 1
        }
      },
      videoFormRules: []
    }
  },
  created() {
    let videoID = this.$route.query && this.$route.query.videoID
    if (videoID !== undefined) {
      videoID = parseInt(videoID)
      if (!isNaN(videoID)) {
        this.listLoading = true
        const formData = {
          'videoID': videoID
        }
        getVideoDetail(formData).then(response => {
          if (response.status === 200) {
            this.videoForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(videoID)
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'video' })
    },
    createData() {
      this.listLoading = true
      const videoForm = this.videoForm
      try {
        videoForm.status = videoForm.status.value
      } catch (error) {
        videoForm.status = 6
      }
      console.log(videoForm)
      if (videoForm.ID > 0) {
        videoForm.videoID = videoForm.ID
      }
      saveVideo(videoForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'video' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      const fromdata = {
        'videoID': row.ID
      }
      removeVideoByID(fromdata).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'video' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
